﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WiMo.Games.Drawables;

namespace WiMo.Games.GameEngine.WP7S.Drawables
{
    public class Rectangle : SpriteShape
    {
        System.Windows.Shapes.Rectangle _rect;

        public Rectangle()
        {
            ShapeType = WiMo.Games.Drawables.ShapeType.Rectangle;
            Origin = Origin.Center;
            Force = Vector2.Zero;
            CollisionShape = CollisionShapes.Rectangle;
            BorderColor = Color.Black;
            BorderWidth = 1;
            FillColor = Color.Transparent;
        }

        public override WiMo.Games.Rectangle Rect
        {
            get { return new WiMo.Games.Rectangle(Position.Point.X - (Size.Width >> 1), Position.Point.Y - (Size.Height >> 1), Size.Width, Size.Height); }
            set
            {
                Size = new Size(value.Width, value.Height);
                Position = new PointF(Rect.Left + Size.Width >> 1, Rect.Top + Size.Height >> 1);
            }
        }

        public override void Render(IDisplay display)
        {
            _rect = new System.Windows.Shapes.Rectangle();
            _rect.Width = Size.Width;
            _rect.Height = Size.Height;

            if (this.BorderColor != Color.Transparent)
            {
                _rect.Stroke = new System.Windows.Media.SolidColorBrush(this.BorderColor.GetMediaColor());
                _rect.StrokeThickness = this.BorderWidth;
            }

            if (this.FillColor != Color.Transparent)
                _rect.Fill = new System.Windows.Media.SolidColorBrush(FillColor.GetMediaColor());

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scale = new ScaleTransform();

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scale);

            _rect.RenderTransform = tg;
            if (Origin == Origin.Center)
            {
                _rect.SetValue(Canvas.TopProperty, (double)(Position.Y - (Size.Height / 2)));
                _rect.SetValue(Canvas.LeftProperty, (double)(Position.X - (Size.Width / 2)));
            }
            else
            {
                _rect.SetValue(Canvas.TopProperty, (double)(Position.Y));
                _rect.SetValue(Canvas.LeftProperty, (double)(Position.X));
            }

            DrawInformation = _rect;

            display.Render(this);
        }
    }
}
